package cn.edu.ujn.finalwork.c;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ujn.finalwork.dao.Base;
import cn.edu.ujn.finalwork.dao.Customer;
import cn.edu.ujn.finalwork.dao.User;
import cn.edu.ujn.finalwork.service.IBaseService;
import cn.edu.ujn.finalwork.service.ICustomerService;

@Controller
public class CustomerController {

    @Autowired
    private ICustomerService customerservice;
    
    @Autowired
    private IBaseService baseService;
    
    @GetMapping("/cus")
    public String first(HttpSession session,Model model) {
        User i = (User)session.getAttribute("userr");
        if(i==null||i.getUserState().equals("0"))
{
            model.addAttribute("fail","请登录账号！");
            return "failure";
        }else if(!(i.getUserAuthority().equals("1"))){
            model.addAttribute("fail","您没有权限！");
            return "failure";
        }else {
        return "index";}
    }
    
    @GetMapping("/goback")
    public String goback() {
        return "index";
    }
    
    
    @GetMapping("/add")//增加客户
    public String add(HttpSession session) {
        List<String> userId = baseService.selectIName("负责人");
        List<String> createId = baseService.selectIName("创建人");
        List<String> source = baseService.selectIName("客户信息来源");
        List<String> industry = baseService.selectIName("客户所属行业");
        List<String> level = baseService.selectIName("客户级别");
        session.setAttribute("u1", userId);
        session.setAttribute("c1", createId);
        session.setAttribute("s1", source);
        session.setAttribute("i1", industry);
        session.setAttribute("l1", level);
        return "addCustomer";
    }
    
    
    @GetMapping("/update")//修改客户
    public String update(HttpSession session) {
        List<String> userId = baseService.selectIName("负责人");
        List<String> createId = baseService.selectIName("创建人");
        List<String> source = baseService.selectIName("客户信息来源");
        List<String> industry = baseService.selectIName("客户所属行业");
        List<String> level = baseService.selectIName("客户级别");
        session.setAttribute("u1", userId);
        session.setAttribute("c1", createId);
        session.setAttribute("s1", source);
        session.setAttribute("i1", industry);
        session.setAttribute("l1", level);
        List<String> u1=new ArrayList<String>();
        for(String u:userId) {
            String i = baseService.selectTCode(u);
            u1.add(i);
        }
        session.setAttribute("u2", u1);
        List<String> u2=new ArrayList<String>();
        for(String u:createId) {
            String i = baseService.selectTCode(u);
            u2.add(i);
        }
        session.setAttribute("c2", u2);
        List<String> u3=new ArrayList<String>();
        for(String u:source) {
            String i = baseService.selectTCode(u);
            u3.add(i);
        }
        session.setAttribute("s2", u3);
        List<String> u4=new ArrayList<String>();
        for(String u:industry) {
            String i = baseService.selectTCode(u);
            u4.add(i);
        }
        session.setAttribute("i2", u4);
        List<String> u5=new ArrayList<String>();
        for(String u:level) {
            String i = baseService.selectTCode(u);
            u5.add(i);
        }
        session.setAttribute("l2", u5);
        return "updateC";
    }
    
    @GetMapping("/delete")//删除客户
    public String delete() {
        return "deleteCustomer";
    }
    
    @GetMapping("/select")//查询客户
    public String select() {
        return "selectCustomer";
    }
    
    @GetMapping("/selectAllCustomer")//查询全部客户
    public String selectAllCustomer(HttpSession session) {
        //客户全部信息
        List<Customer> all = customerservice.selectAll();
        session.setAttribute("all", all);
        List<String> userId=new ArrayList<String>();
        List<String> createId=new ArrayList<String>();
        List<String> source=new ArrayList<String>();
        List<String> induety=new ArrayList<String>();
        List<String> level=new ArrayList<String>();
        for(Customer c:all) {
            Base b=new Base();
            b.setDictTypeCode(c.getCustUserId()+"");
            b.setDictTypeName("负责人");
            Base a = baseService.selectByType_CodeAndType_Name(b);
            userId.add(a.getDictItemName());
            b.setDictTypeName("创建人");
            b.setDictTypeCode(c.getCustCreateId()+"");
            a = baseService.selectByType_CodeAndType_Name(b);
            createId.add(a.getDictItemName());
            b.setDictTypeName("客户信息来源");
            b.setDictTypeCode(c.getCustSource());
            a = baseService.selectByType_CodeAndType_Name(b);
            source.add(a.getDictItemName());
            b.setDictTypeName("客户所属行业");
            b.setDictTypeCode(c.getCustIndustry());
            a = baseService.selectByType_CodeAndType_Name(b);
            induety.add(a.getDictItemName());
            b.setDictTypeName("客户级别");
            b.setDictTypeCode(c.getCustLevel());
            a = baseService.selectByType_CodeAndType_Name(b);
            level.add(a.getDictItemName());
            
        }
        session.setAttribute("userId", userId);
        session.setAttribute("createId", createId);
        session.setAttribute("source", source);
        session.setAttribute("induety", induety);
        session.setAttribute("level", level);
        
        return "allCustomer";
    }
    
    
    @PostMapping("/addOneCustomer")//添加客户
    public String success(@RequestParam("cust_name") String cust_name,@RequestParam("cust_user_id") String cust_user_id,
    @RequestParam("cust_create_id") String cust_create_id ,@RequestParam("cust_sourse") String cust_sourse,
    @RequestParam("cust_industry") String cust_industry,@RequestParam("cust_level") String cust_level,
    @RequestParam("cust_linkman") String cust_linkman,@RequestParam("cust_phone") String cust_phone,
    @RequestParam("cust_moblie") String cust_moblie,@RequestParam("cust_zipcode") String cust_zipcode,
    @RequestParam("cust_address") String cust_address)
    {
        
        Date date=new Date();
        Customer customer = new Customer();
        customer.setCustName(cust_name);
        String selectTCode = baseService.selectTCode(cust_user_id);
        int i=Integer.parseInt(selectTCode);
        customer.setCustUserId(i);
        selectTCode = baseService.selectTCode(cust_create_id);
        i=Integer.parseInt(selectTCode);
        customer.setCustCreateId(i);
        selectTCode = baseService.selectTCode(cust_sourse);
        customer.setCustSource(selectTCode);
        selectTCode = baseService.selectTCode(cust_industry);
        customer.setCustIndustry(selectTCode);
        selectTCode = baseService.selectTCode(cust_level);
        customer.setCustLevel(selectTCode);
        customer.setCustLinkman(cust_linkman);
        customer.setCustPhone(cust_phone);
        customer.setCustMoblie(cust_moblie);
        customer.setCustZipcode(cust_zipcode);
        customer.setCustAddress(cust_address);
        customer.setCustCreatetime(date);
        customerservice.insertSelective(customer);
        
        return "cusSuccess";
        
    }
    
    @PostMapping("/updateOneCustomer")//更新客户
    public String success1(@RequestParam("cust_name") String cust_name,@RequestParam("cust_user_id") String cust_user_id,
    @RequestParam("cust_create_id") String cust_create_id ,@RequestParam("cust_sourse") String cust_sourse,
    @RequestParam("cust_industry") String cust_industry,@RequestParam("cust_level") String cust_level,
    @RequestParam("cust_linkman") String cust_linkman,@RequestParam("cust_phone") String cust_phone,
    @RequestParam("cust_moblie") String cust_moblie,@RequestParam("cust_zipcode") String cust_zipcode,
    @RequestParam("cust_address") String cust_address,HttpSession session)
    {
        Customer c = (Customer)session.getAttribute("cuss");
        Customer customer = new Customer();
        customer.setCustId(c.getCustId());
        customer.setCustName(cust_name);
        int i=Integer.parseInt(cust_user_id);
        customer.setCustUserId(i);
        i=Integer.parseInt(cust_create_id);
        customer.setCustCreateId(i);
        customer.setCustSource(cust_sourse);
        customer.setCustIndustry(cust_industry);
        customer.setCustLevel(cust_level);
        customer.setCustLinkman(cust_linkman);
        customer.setCustPhone(cust_phone);
        customer.setCustMoblie(cust_moblie);
        customer.setCustZipcode(cust_zipcode);
        customer.setCustAddress(cust_address);
        customerservice.updateByPrimaryKeySelective(customer);
        
        
        return "cusSuccess";
        
    }
    @PostMapping("/delete")//删除客户
    public String success2(@RequestParam("cust_name") String cust_name)
    {
        int c=Integer.parseInt(cust_name);
        customerservice.delectByPrimaryKey(c);
        return "cusSuccess";
        
    }
    

    @PostMapping("/selectOneCustomer")//查询客户信息
    public String success2(Model model,@RequestParam("cust_name") String cust_name,@RequestParam("cust_user_id") int cust_user_id,
    HttpSession session)
    {
        
        Customer customer = new Customer();
        customer.setCustName(cust_name);
        customer.setCustUserId(cust_user_id);
        List<Customer> list = customerservice.selective(customer);
        List<String> userId=new ArrayList<String>();
        List<String> createId=new ArrayList<String>();
        List<String> source=new ArrayList<String>();
        List<String> induety=new ArrayList<String>();
        List<String> level=new ArrayList<String>();
        for(Customer c:list) {
            Base b=new Base();
            b.setDictTypeCode(c.getCustUserId()+"");
            b.setDictTypeName("负责人");
            Base a = baseService.selectByType_CodeAndType_Name(b);
            userId.add(a.getDictItemName());
            b.setDictTypeName("创建人");
            a = baseService.selectByType_CodeAndType_Name(b);
            createId.add(a.getDictItemName());
            b.setDictTypeName("客户信息来源");
            a = baseService.selectByType_CodeAndType_Name(b);
            source.add(a.getDictItemName());
            b.setDictTypeName("客户所属行业");
            a = baseService.selectByType_CodeAndType_Name(b);
            induety.add(a.getDictItemName());
            b.setDictTypeName("客户级别");
            a = baseService.selectByType_CodeAndType_Name(b);
            level.add(a.getDictItemName());
            
        }
        session.setAttribute("userId", userId);
        session.setAttribute("createId", createId);
        session.setAttribute("source", source);
        session.setAttribute("induety", induety);
        session.setAttribute("level", level);
          
        if(list.isEmpty()) {
            model.addAttribute("fail","该数据不存在！");
            return "failure";
        }else {
          session.setAttribute("tiaojian", list);

        return "customer";//客户信息页面  
        }
        
        
    }
    
    @PostMapping("/selectC")//update中先查找然后赋值到表格中进行更改
    public String selectC(Model model,HttpSession session,@RequestParam("cust_id") String cust_id){
        int i=Integer.parseInt(cust_id);
        Customer key = customerservice.selectByPrimaryKey(i);
        if(key==null) {
            model.addAttribute("fail","该数据不存在！");
            return "failure";
        }else {
          session.setAttribute("cuss", key);
        return "updateCustomer";  
        }
        
    }
    
    
}
